package com.tc.dispatch.web.schedule.order;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.tc.dispatch.gsservice.ejb.GSServiceLocal;
import com.tc.dispatch.gsservice.exception.GSException;
import com.tc.dispatch.gsservice.model.OrderSumSub;
import com.tc.dispatch.gsservice.model.ProcessStep;
import com.tc.dispatch.web.gsservice.gsHelper;
import com.tc.dispatch.web.util.DateJsonValueProcessor;
import com.tc.dispatch.web.util.tools;

public class processStep extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public processStep() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html; charset=utf-8");
		//response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String action=request.getParameter("action");
	      GSServiceLocal gss=gsHelper.getGSServiceLocal();
	      if(action.equals("list"))
	      {
	          String page=request.getParameter("start");
	          if(page==null)
	            page="0";
	          String pagePerNum=request.getParameter("limit");
	          if(pagePerNum==null)
	            pagePerNum="10";
	            try{
	              List ret = gss.findProcessStepByPages(Integer.parseInt(page),
	                                                     Integer.parseInt(pagePerNum));
	              System.out.println(page+"::"+pagePerNum);
	              JSONObject json = new JSONObject();
	              JsonConfig cfg = new JsonConfig();
	              cfg.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
	              cfg.registerJsonValueProcessor(java.sql.Date.class, new DateJsonValueProcessor());


	             // BeanUtils.copyProperties();
	              JSONArray arr = JSONArray.fromObject(ret.toArray(),cfg);
	              json.put("root", arr);
	              response.getWriter().print(json);
	            }
	            catch(GSException e)
	            {
	              response.sendRedirect("orderError.jsp?error="+e.getErrorCode());
	            }
	      }
	      else if(action.equals("new"))
	      {
	        int id=0;
	        try{
	     String specName=request.getParameter("specName");
	     String processName=request.getParameter("processName");
	     int processPos=Integer.parseInt(request.getParameter("processPos"));
	     int scheme=Integer.parseInt(request.getParameter("scheme"));
	     java.util.Date date1=tools.stringToDate(request.getParameter("date1"));
	     String bz=request.getParameter("bz");
	     String areaFunc=request.getParameter("areaFunc");
	     ProcessStep osm=new ProcessStep(id, specName,   scheme,processName, processPos,bz,  date1,areaFunc);
	     gss.createProcessStep(osm);
	        }
	        catch(NumberFormatException nfe)
	        {
	          System.out.print("numberfomat error!:"+nfe.getMessage());
	        }
	        catch(GSException gse)
	        {
	          response.sendRedirect("orderError.jsp?error="+gse.getErrorCode());
	        }
	        response.getWriter().print("{success:true}");

	      }
	      else if(action.equals("update"))
	      {

	        try{
	      int id=Integer.parseInt(request.getParameter("id"));
	      String specName=request.getParameter("specName");
		     String processName=request.getParameter("processName");
		     int processPos=Integer.parseInt(request.getParameter("processPos"));
		     int scheme=Integer.parseInt(request.getParameter("scheme"));
		     java.util.Date date1=tools.stringToDate(request.getParameter("date1"));
		     String bz=request.getParameter("bz");
		     String areaFunc=request.getParameter("areaFunc");
		     ProcessStep osm=new ProcessStep(id, specName,   scheme,processName, processPos,bz,  date1,areaFunc);
		 		     gss.updateProcessStep(osm);
	        }
	        catch(NumberFormatException nfe)
	        {
	          System.out.print("numberfomat error!:"+nfe.getMessage());
	        }
	        catch(GSException gse)
	        {
	          response.sendRedirect("orderError.jsp?error="+gse.getErrorCode());
	        }
	        response.getWriter().print("{success:true}");

	      }
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
